import { asyncRouterMap, constantRouterMap, asyncOnduty } from '@/config/router.config'
import router from '@/router/index';

/**
 * 过滤账户是否拥有某一个权限，并将菜单从加载列表移除
 *
 * @param permission
 * @param route
 * @returns {boolean}
 */
function hasPermission(permission, route) {
  // console.log(permission, route);
  if (route.meta && route.meta.permission) {
    let flag = false
    for (let i = 0, len = permission.length; i < len; i++) {
      flag = route.meta.permission.includes(permission[i])
      if (flag) {
        return true
      }
    }
    return false
  }
  return true
}

/**
 * 单账户多角色时，使用该方法可过滤角色不存在的菜单
 *
 * @param roles
 * @param route
 * @returns {*}
 */
// eslint-disable-next-line
function hasRole(roles, route) {
  if (route.meta && route.meta.roles) {
    return route.meta.roles.includes(roles.id)
  } else {
    return true
  }
}

function filterAsyncRouter(routerMap, roles) {
  // console.log(routerMap, roles);
  const accessedRouters = routerMap.filter(route => {
    // console.log(route);
    if (hasPermission(roles.permissionList, route)) {
      if (route.children && route.children.length) {
        route.children = filterAsyncRouter(route.children, roles)
      }
      return true
    }
    return false
  })
  return accessedRouters
}
//根据登录获取到的菜单手动筛选可以查看的
function filterRouter(routerMap, roles) {
  // console.log('所有路由树', routerMap);
  // console.log('用户路由树', roles);
  // return roles
  let routers = []
  routerMap.forEach(item => {
    if (item.redirect == '/404') {
      routers.push(item)
    }
  })
  const parent = routerMap.find(({ path }) => '/' == path)
  if (parent && Array.isArray(parent.children)) {
    const result = {
      ...parent,
      children: []
    }
    const handleGetChildRoute = (parent, childMap, childs) => {
      childs.forEach(route => {
        const child = childMap.find(({ path }) => route.webUrl == path)
        if (child&&child.meta&&child.meta.title) {
          child.meta.title = route.name
        }
        if (child) {
          const result = {
            ...child,
            children: []
          }
          parent.children.push(child)
          if (Array.isArray(child.children) && Array.isArray(route.childList)) {
            handleGetChildRoute(result, child.children, route.childList)
          }
        }
      })
    }
    roles.forEach(route => {
      const child = parent.children.find(({ path }) => route.webUrl == path)
      if (child&&child.meta&&child.meta.title) {
        child.meta.title = route.name
      }

      console.log('route.logoUrl ',route.logoUrl )
      if(child){
        console.log('child.meta.icon ',child.meta.icon )

      }
      // if (route.logoUrl != '') {
      //   child.meta.icon = route.logoUrl
      // }

      if (child) {
        const obj = {
          ...child,
          children: []
        }
        if (Array.isArray(child.children) && Array.isArray(route.childList)) {
          handleGetChildRoute(obj, child.children, route.childList)
        }
        result.children.push(obj)
      }
    })

    routers.push(result)
  }
  // })
  console.log('返回的路由===', routers);
  return routers
}




const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    GenerateRoutes({ commit }, data) {
      console.log('permission---',data)
      return new Promise(resolve => {
        const { roles } = data
        // console.log({ roles });
        const accessedRouters = filterRouter(asyncRouterMap, roles)

        // console.log(accessedRouters);
        commit('SET_ROUTERS', accessedRouters)

        resolve()
      })
    }
  }
}

export default permission
